﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GovernmentOfficeProject.LevelDataAccessModel;
using GovernmentOfficeProject.LevelDataAccess;
using System.Collections;
using GovernmentOfficeProject.SysTools;
namespace GovernmentOfficeProject.LevelBusinessLogic
{
    /// <summary>
    /// 系统相关逻辑 包括：菜单、按钮等
    /// </summary>
    public class BLL_SYSTEM
    {
        /// <summary>
        /// 根据用户权限获得菜单列表
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public List<Entity_SYS_Menu> getMenuListByUser(decimal userId)
        {
            DAL_SYS_Menu dalMenu = new DAL_SYS_Menu();
            List<Entity_SYS_Menu> list = null;
            try
            {
                list = dalMenu.getMenuListByUser(userId);

            }
            catch (Exception e)
            {
                throw new Exception(e.Message, e);
            }
            return list;
        }



        /// <summary>
        /// 
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public string GetMenuListByUserToJson(decimal userId)
        {
            List<Entity_SYS_Menu> list = getMenuListByUser(userId);
            //存储一级菜单
            List<Entity_SYS_Menu> rootMenuList = new List<Entity_SYS_Menu>();
            //以树形结构存储菜单
            Dictionary<string, TreeMenu> map = new Dictionary<string, TreeMenu>();
            //JSON 格式数据
            Dictionary<string, Object> menuInfoMap = new Dictionary<string, Object>();

            DAL_USER_User dalUser  = new DAL_USER_User();

            List<Entity_USER_User> userList = dalUser.getUserListByRelation(userId);

            //存储‘领导日程’菜单的MenuNo
            string ldMenuNo = "";
            foreach (Entity_SYS_Menu menu in list)
            {
                if(menu.MenuName == "领导日程")
                {
                    ldMenuNo = menu.MenuNo;
                }

                if (menu.MenuIsLeaf == 0)
                {
                    rootMenuList.Add(menu);
                }

                TreeMenu treeMneu = null;
                if (map.ContainsKey(menu.MenuParentNo))
                {
                    treeMneu = map[menu.MenuParentNo]; 
                }
                 
                if (treeMneu != null)
                {
                    TreeMenu subMenu = new TreeMenu();
                    subMenu.id = menu.MenuID;
                    subMenu.MenuName = menu.MenuName;
                    subMenu.MenuNo = menu.MenuNo;
                    subMenu.url = menu.MenuURL;
                    treeMneu.childrens.Add(subMenu);
                }
                else
                {
                    treeMneu = new TreeMenu();
                    treeMneu.id = menu.MenuID;
                    treeMneu.MenuName = menu.MenuName;
                    treeMneu.MenuNo = menu.MenuNo;
                    treeMneu.url = menu.MenuURL;
                    map.Add(menu.MenuNo, treeMneu);
                }
            }
            menuInfoMap.Add("rootMenu", rootMenuList);
            menuInfoMap.Add("menuDetails", map);

            //添加领导日程子节点
            TreeMenu ldTreeMenu = null;
            foreach (string key in map.Keys)
            {
                if (TreeMenu.getNodeByName(ldMenuNo, map[key]) != null){
                    ldTreeMenu = TreeMenu.getNodeByName(ldMenuNo, map[key]);
                    break;
                } 
            }

            if (ldTreeMenu != null && userList.Count > 0)
            {
                foreach(Entity_USER_User user in userList)
                {
                    TreeMenu tm = new TreeMenu();
                    tm.id = user.UserID;
                    tm.MenuName = user.RealName;
                    tm.url = "/LevelViewUI/calendar/CalendarMain.aspx?ldCalendar=true&userId=" + user.UserID;
                    ldTreeMenu.childrens.Add(tm);
                }
            }

            
            return JsonHelper.ToJson(menuInfoMap);

        }

        /// <summary>
        /// 根据菜单编码和用户ID，获得有权限的按钮
        /// 如果menuNo为null 则视为获取领导日程菜单权限
        /// </summary>
        /// <param name="menuNo"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static string GetMenuButtonByMneuNoAndUserIdToJsion(decimal userId,string menuNo)
        {
            DAL_SYS_Button dalButton = new DAL_SYS_Button();
            List<Entity_SYS_Button> buttonList = null;
            //领导日程菜单
            if(menuNo == null)
            {
                buttonList = dalButton.getButtonListByRelation(SystemContext.CurrentUserID,userId);
            }
            //其他菜单
            else
            {
                buttonList = dalButton.getButtonListByMenu(userId, menuNo);
            }
            
            string buttonStr = "";
            foreach (Entity_SYS_Button button in buttonList)
            {
                buttonStr = buttonStr+button.BtnNo+",";
            }
            return JsonHelper.ToJson(buttonStr);
        }

        /// <summary>
        /// 根据用户ID获得关联用户 用于显示领导日程的子菜单
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static string GetUserRelationToJsion(decimal userId)
        {
            DAL_USER_User dalUser = new DAL_USER_User();
            List<Entity_USER_User> userList = new List<Entity_USER_User>();//dalUser.getUserListByRelation(userId);
            return JsonHelper.ToJson(userList);
        }

        /// <summary>
        /// 获得所有menu(模块、子模块)
        /// </summary>
        /// <returns></returns>
        public List<Entity_SYS_Menu> getAllMenu()
        {
            try
            {
                return new DAL_SYS_Menu().getMenuList();
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获得所有button(功能)
        /// </summary>
        /// <returns></returns>
        public List<Entity_SYS_Button> getAllButton()
        {
            try
            {
                return new DAL_SYS_Button().getButtonList();
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

        public List<Entity_SYS_Menu> getMenuListByCondition(Entity_SYS_Menu menuEntity)
        {
            try
            {
                return new DAL_SYS_Menu().getMenuListByMenuEntity(menuEntity);
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

        public List<Entity_SYS_Button> getButtonListByCondition(Entity_SYS_Button buttonEntity)
        {
            try
            {
                return new DAL_SYS_Button().getButtonListByButtonEntity(buttonEntity);
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

        public Entity_SYS_Menu getMenuByMenuID(decimal menuID)
        {
            try
            {
                return new DAL_SYS_Menu().getMenuByMenuID(menuID);
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

        public Entity_SYS_Button getButtonByButtonID(decimal buttonID)
        {
            try
            {
                return new DAL_SYS_Button().getButtonByButtonID(buttonID);
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

        //public string getOnleMenuNoByParentNo(string parentNo)
        //{
        //    try
        //    {
        //        string strMenuNo = "";
        //        Entity_SYS_Menu menuEntity = new Entity_SYS_Menu();
        //        menuEntity.MenuParentNo = parentNo;
        //        List<Entity_SYS_Menu> menuList = new DAL_SYS_Menu().getMenuListByMenuEntity(menuEntity);
        //        foreach (Entity_SYS_Menu menu in menuList)
        //        {
        //            strMenuNo = menu.MenuNo;
        //            AppLog.Write("MenuNo=" + menu.MenuNo, AppLog.LogMessageType.Debug);
        //        }
        //        return strMenuNo;
        //    }
        //    catch (System.Exception ex)
        //    {
        //        throw ex;
        //    }
        //}

    }
}